package com.lvcoding.common;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author wuyanshen
 * @discription 描述
 * @date 2021-02-24 上午10:46
 */
@Component
@AllArgsConstructor
public class TokenConfig {

    private final RedisConnectionFactory redisConnectionFactory;

    // private final UserDetailsService userDetailsService;

    // private final JwtAccessTokenConverter jwtAccessTokenConverter;

    // @Bean
    // public TokenStore tokenStore() {
    //     return new JwtTokenStore(jwtAccessTokenConverter);
    // }
    //
    // @Bean
    // public JwtAccessTokenConverter jwtAccessTokenConverter() {
    //     final JwtAccessTokenConverter jwtAccessTokenConverter = new OauthJwtAccessTokenConverter(userDetailsService);
    //     // final JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    //     jwtAccessTokenConverter.setSigningKey("oauth2-demo");
    //     return jwtAccessTokenConverter;
    // }

    /**
     * 内存存储token
     */
    // @Bean
    // public TokenStore tokenStore() {
    //     return new InMemoryTokenStore();
    // }

    /**
     * redis存储token
     */
    @Bean
    public TokenStore tokenStore() {
        final RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        redisTokenStore.setPrefix("oauth2:demo:");
        return redisTokenStore;
    }
}
